package com.jiang.books.service.jpa;

import java.math.BigInteger;
import java.util.List;

import org.apache.log4j.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.jiang.books.common.utils.annotation.AdditionalLogging;
import com.jiang.books.domain.Award;
import com.jiang.books.repository.AwardRepository;
import com.jiang.books.service.AwardService;

@Service("jpaAwardService")
@Repository
@Transactional
public class AwardServiceImpl implements AwardService {

	private static final Logger log = Logger.getRootLogger();
	
	//@PersistenceContext
	//private EntityManager entityManager;
	
	@Autowired
	private AwardRepository repository;
	
	@Override
	@AdditionalLogging
	@Transactional(readOnly=true)
	public List<Award> findAll() {
		log.info("Award#Find All Awards");
        return Lists.newArrayList(repository.findAll());
	}

	@Override
	@Transactional(readOnly=true)
	public Award findById(BigInteger id) {
		log.info("Award#Find Award by id: " + id);
        return repository.findOne(id);
	}

	@Override
	public Award save(Award award) {
		log.info("Award#Save Award with id: " + (!award.isNew() ? award.getId() : "not yet generated"));
        return repository.save(award);
	}

	@Override
	public void delete(Award award) {
        repository.delete(award);
        log.info("Award#Award with id: " + award.getId() + " deleted successfully");
	}
}